package com.friend.controller.user;

import com.common.core.constants.HttpConstants;
import com.common.core.controller.BaseController;
import com.common.core.domain.Result;
import com.common.core.domain.vo.LoginUserVO;
import com.friend.domain.user.dto.UserDTO;
import com.friend.domain.user.dto.UserUpdateDTO;
import com.friend.domain.user.vo.UserVO;
import com.friend.service.user.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Resource
    UserService userService;

    /**
     *发送手机验证码
     *
     */
    @PostMapping("/sendCode")
    public Result<Void> sendCode(@RequestBody UserDTO userDTO){
        return toResult(userService.sendCode(userDTO));
    }

    /**
     * 用户登录
     *
     */
    @PostMapping("/code/login")
    public Result<String> codeLogin(@RequestBody UserDTO userDTO){
        return Result.success(userService.codeLogin(userDTO.getPhone(),userDTO.getCode()));
    }

    /**
     * 退出登录
     *
     */
    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return toResult(userService.logout(token));
    }

    /**
     * 获取用户信息
     *
     */
    @GetMapping("/info")
    public Result<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return userService.info(token);
    }

    /**
     * 获取用户详情
     *
     */
    @GetMapping("/detail")
    public Result<UserVO> detail(){
        return userService.detail();
    }

    /**
     * 编辑个人信息
     *
     */
    @PutMapping("/edit")
    public Result<Void> edit(@RequestBody UserUpdateDTO userUpdateDTO){
        return toResult(userService.edit(userUpdateDTO));
    }

    /**
     * 上传用户头像
     *
     */
    @PutMapping("/head-image/update")
    public Result<Void> updateHeadImage(@RequestBody UserUpdateDTO userUpdateDTO){
        return toResult(userService.updateHeadImage(userUpdateDTO.getHeadImage()));
    }
}
